import request from '/shop/request';
import { baseUrl } from '/shop/config';
import aliOssSts from '/shop/api/aliOssSts.js';

function sts(data) {
	return request({
		url: '/api/file/sts',
		method: 'POST',
		data,
	});
}

export default {
	// 系统初始化
	init: (templateId, platform = 'h5') =>
		request({
			url: '/api/home/init',
			params: {
				templateId,
				platform,
			},
			custom: {
				showError: false,
				showLoading: false,
			},
		}),
	// 同步客户端页面到后端
	pageSync: (pages) =>
		request({
			url: 'pageSync',
			method: 'POST',
			data: {
				pages,
			},
			custom: {
				showError: false,
				showLoading: false,
			},
		}),
	// 发送短信
	sendSms: (data) =>
		request({
			url: '/send/sms',
			method: 'POST',
			data,
			custom: {
				showSuccess: true,
				loadingMsg: '发送中',
			},
		}),
	//意见反馈
	feedback: (data) =>
		request({
			url: '/api/feedback/create',
			method: 'POST',
			data,
		}),
	// 自定义页面
	page: (id) =>
		request({
			url: 'page/' + id,
			method: 'GET',
		}),
	//积分商城
	scoreShop: {
		list: (data) =>
			request({
				url: '/api/scoreshop/search',
				method: 'POST',
				data,
			}),
		goodsIds: (data) =>
			request({
				url: '/api/scoreshop/list/goodsids',
				method: 'POST',
				data,
			}),
		detail: (id) =>
			request({
				url: '/api/scoreshop/detail/' + id,
				method: 'GET',
			}),
	},
	//小程序直播
	mplive: {
		getRoomList: (ids) =>
			request({
				url: '/api/app/mplive/getRoomList',
				method: 'GET',
				params: {
					ids: ids.join(','),
				},
			}),
		getMpLink: () =>
			request({
				url: '/api/app/mplive/getMpLink',
				method: 'GET',
			}),
	},

	//上传
	upload: (file, group = 'ugc', callback) => {
		const token = uni.getStorageSync('token');
		uni.showLoading({
			title: '上传中',
		});
		return new Promise(async (resolve, reject) => {
			let { data } = await sts({ fileName: file.name || file });
			let option = {
				KeyPath: data.key,
				AccessKeySecret: data.accessKeySecret,
				SecurityToken: data.securityToken,
				AccessKeyId: data.accessKeyId,
				Url: data.url,
			};
			await aliOssSts.upload(file.path || file, option);
			console.log('上传完成.....: ', data.key);
			resolve({ path: data.key });
			// uni.uploadFile({
			// 	url: baseUrl + '/file/upload',
			// 	filePath: file.path,
			// 	name: 'file',
			// 	formData: {
			// 		group,
			// 	},
			// 	header: {
			// 		Accept: 'application/json',
			// 		Authorization: token,
			// 	},
			// 	success: (uploadFileRes) => {
			// 		console.log('uploadFileRes: ', uploadFileRes);
			// 		let result = JSON.parse(uploadFileRes.data);
			// 		console.log('result: ', result);
			// 		// let result = uploadFileRes.data;
			// 		if (result.code === '0') {
			// 			console.log('result.data: ', result.data);
			// 			resolve(result.data);
			// 		} else {
			// 			uni.showToast({
			// 				icon: 'none',
			// 				title: result.msg,
			// 			});
			// 		}
			// 	},
			// 	fail: (error) => {
			// 		console.log('上传失败：', error);
			// 		resolve(false);
			// 	},
			// 	complete: () => {
			// 		uni.hideLoading();
			// 	},
			// });
		});
	},
};
